using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SQLite;
using System.IO;
using log4net;
using LearningEnglishProgram.UI;

namespace LearningEnglishProgram.DAO
{
    class BaseDAO
    {
        ILog log = Log.GetLog("BaseDAO");

        protected String m_sConnection = "Data Source=./Database/EnglishDB.db;Version=3;Password=123456;";
        protected SQLiteConnection m_con = null;

        public BaseDAO ()
        {
            try
            {
                m_con = this.openConnection();
            }
            catch (SQLiteException e)
            {
                log.Error(e);
            }
            
        }





        private SQLiteConnection openConnection()
        {
            SQLiteConnection res = null;
            try
            {
                res = new SQLiteConnection(m_sConnection);
            }
            catch (SQLiteException e)
            {
                throw new SQLiteException("Error: ", e);
            }
            
            return res;
        }

        protected bool closeConnection()
        {
            if (m_con != null)
            {
                m_con.Close();
                return true;
            }

            return false;
        }





        /// <summary>
        /// Get SQL script's data from SQL file
        /// </summary>
        /// <param name="sFileName">SQL file name</param>
        /// <returns>Success: script's content. Otherwise, return blank string</returns>
        //protected String getSQLScript(String sFileName)
        //{
        //    FileInfo scriptFile = new FileInfo(m_sScriptPath + sFileName);

        //    String sRes = "";
        //    StreamReader reader = null;

        //    try
        //    {
        //        reader = new StreamReader(scriptFile.FullName);
        //        sRes = reader.ReadToEnd();
        //    }
        //    catch (IOException e)
        //    {
        //        log.Error(e);
        //    }
        //    finally
        //    {
        //        if (reader != null)
        //            reader.Close();
        //    }

        //    return sRes;
        //}

        //protected String getSQLScript(String sFileName, Object[] arrParams)
        //{
        //    return null;
        //}

    }
}
